package com.fairytask.ui.base
{
	import com.fairytask.engine.GameStage;
	import com.fairytask.engine.components.button.Button;
	import com.fairytask.engine.define.UIDefine;
	import com.fairytask.managers.ViewManage;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	/**
	 * 基础窗体
	 * @author Hanjy
	 *
	 */
	public class FrameUI extends BaseUI
	{
		//显示容器
		public var _btnSpace	:Sprite	=new Sprite();	//按钮显示部分
//		private var _uiSpace	:Sprite	=new Sprite();	//内容显示部分
		public var _unitUI	:UnitUI = new UnitUI;	//内容显示部分
		
		//数据
		private var _btnData	:Array	=	[];		//按钮数据
		private var _uiData	:Array	=	[];		//点击按钮显示的UI
		private var _uiSkin	:Array	=	[];		//皮肤
		private var _btnBlank	:int	=	5;		//按钮间隔
		private var _btnIndex	:int	=	0;		//按钮位置
		
		private var _initX			:int	=	0;
		private var _initY			:int	=	0;
		
		//npc地址记录
		protected var npcPoint		:Point	=	null;
		//是否绑定
		protected var isBind		:Boolean	=	false;
		//是否自动定位
		public var autoPos			:Boolean	=	true;
		//自动发送打开界面消息
		public var autoSendMsg		:Boolean	=	true;


		//是否为可关闭窗口
		private var _popupUI:Boolean=true;
		//常量
		public static const BACKLAYER		:String = "back";
		public static const MIDDLELAYER	:String = "middle";
		public static const LOGINLAYER	:String = "login";
		public static const TOPLAYER		:String = "top";
		public static const LOADLAYER		:String = "load";
		//当前位置
		protected var currentLayer	:String	=	MIDDLELAYER;
		//当前选择显示按钮
		private var _curBtnPos	:String	= "";
		//当前页
		private var _curPos	:int = -1;
		private var _isShow	:Boolean;	//部分界面（非mid层）有效，只读
		
		public function FrameUI(popUI:Boolean=true)
		{
			super();
			_popupUI	=	popUI;
			
			autoPos	=	popUI;
			
			initX = this.x;
			initY = this.y;
			addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			GameStage.stage.addEventListener(Event.RESIZE,stageReSize);
			if(windowBG!=null)
			{
				windowBG.addChild(_unitUI);
				windowBG.addChild(_btnSpace);
				_btnSpace.x = 25;
				_btnSpace.y = 20;
				_unitUI.x = 15;
				_unitUI.y = 35;
			}
			
		}
		
		/**
		 * 框架UI隐藏 
		 * @param e
		 * 
		 */
		override public function hide(e:*=null):void
		{
			ViewManage.maskEnable	=	false;
			if(uiName&&UIDefine[uiName])
			{
				//通知服务器关闭
				//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_CLOSE_UI,UIDefine[uiName]);
			}
			if(uiManage.uiArray.indexOf(this)!=-1)
			{
				uiManage.uiArray.unshift(this);
				uiManage.uiArray.shift();
			}
			AfterHide();
			if (parent != null)
			{
				removeUI(this);
			}
			if(_btnData.length > 0 && _curPos != -1)
			{
				(_btnData[_curPos] as Button).isSelect = false;
			}
			_curPos = -1;
			_isShow = false;
		}

		/**
		 * 显示界面  
		 * @param space 显示层次
		 * @param onshow 显示后回调
		 */
		override public function show(space:String=MIDDLELAYER, onshow:Function=null):void
		{
			BeforeShow();
			if((windowBG!=null))
			{
				windowBG.onDragStop(null);				
			}
			if (_popupUI)
			{
				if(uiManage.uiArray.indexOf(this)==-1)
				{
					uiManage.uiArray.push(this);
				}
			}
			//IO.traceLog(_uiManage._uiArray.length + "UI 界面在数量", this, "Class->", (this as Object).constructor);
			onshowFunc=onshow;
			switch(space)
			{
				case BACKLAYER:
					GameStage.uiSpace.addBack(this);
					break;
				case MIDDLELAYER:
					GameStage.uiSpace.addMiddle(this);
					break;
				case LOGINLAYER:
					GameStage.uiSpace.addLogin(this);
					break;
				case TOPLAYER:
					GameStage.uiSpace.addTop(this);
					break;
				case LOADLAYER:
					if(uiName == "GuidePopUI")
					{
						GameStage.uiSpace.addLoad(this,1);
					}
					else
					{
						GameStage.uiSpace.addLoad(this);
					}
					break;
				default:
					GameStage.uiSpace.addMiddle(this);
					break;
			}
			currentLayer	=	space;
			
			
			if(!hasEventListener(Event.ADDED_TO_STAGE))
			{
				this.addEventListener(Event.ADDED_TO_STAGE, onAddHandle);
			}
			
			if (onshowFunc != null)
			{
				onshowFunc.call(this, this);
			}
			//检测一下是不是要发子UI的打开
			/*if(this._unitUI.currentUI!=null)
			{
				//取得子UI名称
				if(_unitUI.currentUI.uiName&&UIDefine[_unitUI.currentUI.uiName])
				{
					//通知服务器打开
					//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_OPEN_UI,UIDefine[_unitUI.currentUI.uiName]);
				}
			}*/
			
			if(uiName&&UIDefine[uiName]&&autoSendMsg)
			{
				//通知服务器打开
				//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_OPEN_UI,UIDefine[uiName]);
			}
			//有内容默认打开第一页
			if(_btnData.length>0 && _uiData.length>0)
			{
				_curBtnPos = "";
				showPageUI(0);
			}
			
			ViewManage.maskEnable	=	popupUI;
			
			if(autoPos)
			{
				this.x	=	(GameStage.stageWidth-windowBG.width-GameStage.x*2)/2;
				this.y	=	(GameStage.stageHeight-windowBG.height-GameStage.y*2)/2;
			}
			_isShow = true;
		}
		
		
		/**
		 * 当添加到舞台上要做的操作 
		 * 
		 */
		protected function onAdd():void
		{

		}

		/**
		 * 添加到舞台事件 
		 * @param e
		 * 
		 */
		private function onAddHandle(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, onAddHandle);
			if (onshowFunc != null)
			{
				onshowFunc.call(this, this);
			}
			onAdd();
		}

		/**
		 * 鼠标点击 
		 * @param evt
		 * 
		 */
		private function onMouseDown(evt:MouseEvent):void
		{
			if (_popupUI&&currentLayer==MIDDLELAYER)
			{
				this.parent.setChildIndex(this, this.parent.numChildren - 1);
			}
		}
		
		/**
		 *	交换UI深度 
		 * 
		 */		
		public function swapDepth():void
		{
			onMouseDown(null);
		}
		
		/**
		 * 显示前处理 
		 * 
		 */
		protected function BeforeShow():void
		{
			stageReSize();
		}
		
		/**
		 * 场景大小改变 
		 * @param evt
		 * 
		 */
		protected function stageReSize(evt:Event=null):void
		{
			
		}
		
		/**
		 * 是否可拖动 
		 * @param val
		 * 
		 */
		public function set dragEnable(val:Boolean):void
		{
			if(val)
			{
				windowBG.addDrag();
			}
			else
			{
				windowBG.removeDrag();
			}
		}
		
		/**
		 * 设置按钮空白间隔 
		 * @param val
		 * 
		 */
		public function set btnBlank(val:int):void
		{
			_btnBlank = val;
		}
		
		/**
		 * 调整按钮位置 
		 * @param btnX
		 * @param btnY
		 * 
		 */
		public function setBtnPos(btnX:int,btnY:int):void
		{
			_btnSpace.x = btnX;
			_btnSpace.y = btnY;
		}
		
		/**
		 * 调整UnitUI位置 
		 * @param unitX
		 * @param unitY
		 * 
		 */
		public function setUnitPos(unitX:int,unitY:int):void
		{
			_unitUI.x = unitX;
			_unitUI.y = unitY;
		}
		
		/**
		 * 清除按钮 
		 * 
		 */
		public function clearButton():void
		{
			clearSp(_btnSpace);
			_btnIndex = 0;
		}
		
		/**
		 * 添加按钮
		 * @param btn 按钮
		 * 
		 */
		public function addButton(btn:Button):void
		{
			//设置按钮X位置
			_btnIndex == 0 ? btn.x = 0: btn.x = _btnSpace.width + _btnBlank 
			
			//将按钮加入数据
			_btnData.push(btn);
			//按钮位置编号
			btn.name = _btnIndex.toString();
			_btnIndex++;
			//把按钮添加到容器
			_btnSpace.addChild(btn);
			btn.addEventListener(MouseEvent.CLICK,onClick);
		}
		
		/**
		 * 点击顶部按钮，显示相对应的UI 
		 * @param evt
		 * 
		 */
		private function onClick(evt:MouseEvent):void
		{
			if(_curBtnPos != evt.currentTarget.name)
			{
				_curBtnPos = evt.currentTarget.name;
				showPageUI(int(_curBtnPos));
			}
		}
		
		/**
		 * 显示对应位置UI 
		 * @param pos
		 * 
		 */
		private function showPageUI(pos:int):void
		{
			//显示位置相同就不刷新了
			if(_curPos == pos || pos == -1)
			{
				return;
			}
			//先清除UI容器内容
			_unitUI.clearUI();
			if(pos<_uiData.length)
			{
				_unitUI.curUIName = uiName;
				_unitUI.showUI(_uiData[pos],_uiSkin[pos]);
			}
			setBtnState(pos);
		}
		
		/**
		 * 标签页显示 
		 * 
		 */
		private function setBtnState(pos:int):void
		{
			if(_curPos != -1)
			{
				(_btnData[_curPos] as Button).isSelect = false;
			}
			(_btnData[pos] as Button).isSelect = true;
			_curPos = pos;
		}
		
		/**
		 * 直接显示打开pos位置UI 
		 * @param pos
		 * 
		 */
		public function showPos(pos:int):void
		{
			show();
			showPageUI(pos);
		}
		
		/**
		 * 添加duUI数据 
		 * @param val UI界面
		 * @param index 对应的按钮位置
		 * 
		 */
		public function addShowUI(val:Array,index:int,skin:Array=null):void
		{
			_uiData[index] = val;
			_uiSkin[index] = skin;
		}
		
		/**
		 * 取对应按钮 
		 * @param val
		 * @return 
		 * 
		 */
		public function getButtonByIndex(val:int):Button
		{
			var tmpBtn:Button;
			if(val>=0 && val<_btnData.length)
			{
				tmpBtn = _btnData[val];
			}
			return tmpBtn;
		}
		
		/**
		 * 清除容器内容 
		 * @param val
		 * 
		 */
		private function clearSp(val:Sprite):void
		{
			while(val.numChildren>0)
			{
				val.removeChildAt(0);
			}
		}
		
		/**
		 * 检查是否为相应的UI 
		 * @param val
		 * @return 
		 * 
		 */
		public function checkUI(val:int):Boolean
		{
			for each(var arr:Array in _uiData[_curPos])
			{
				if(arr[0] == val)
				{
					return true;
				}
			}
			return false;
		}
		
		/**
		 * 界面是否打开 
		 * @param pos
		 * @return 
		 * 
		 */
		public function isOpen(pos:int=-1):Boolean
		{
			if(null == this.parent)
			{
				return false;
			}
			else
			{
				if(pos != -1 && pos != _curPos)
				{
					return false;
				}
				else if(_uiData.length != 0 && pos == -1 && _curPos != 0)
				{
					return false;
				}
				else
				{
					return true;
				}
			}
		}
		
		//代码生成器
		//===================================================================================================================		
		public function get initX():int
		{
			return _initX;
		}
		public function set initX(value:int):void
		{
			_initX = value;
		}
		public function get initY():int
		{
			return _initY;
		}
		public function set initY(value:int):void
		{
			_initY = value;
		}

		public function get popupUI():Boolean
		{
			return _popupUI;
		}

		public function set popupUI(value:Boolean):void
		{
			_popupUI = value;
		}
		
		public function get isShow():Boolean
		{
			return _isShow;
		}

	}
}